﻿using DotNetCore.CAP;
using Microsoft.Extensions.Hosting;
using Solution.Application.Dtos;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Solution.Application.Workers
{
    public class RabbitMQCapPublisher : BackgroundService
    {
        private readonly ICapPublisher _eventBus;

        public RabbitMQCapPublisher(ICapPublisher eventBus)
        {
            _eventBus = eventBus;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Console.WriteLine("start create order by CapPublishWorker");
            for (int i = 0; i < 10; i++)
            {
                await CreateOrderAsync(Guid.NewGuid().ToString());
            }
        }

        private async Task CreateOrderAsync(string productName)
        {
            var request = new EventInput<CreateOrderInput>()
            {
                Data = new CreateOrderInput()
                {
                    ProductName = productName,
                    UserId = 0,
                    CreateOrderTime = DateTime.UtcNow,
                }
            };

            await _eventBus.PublishAsync("cap.create.order.queue", request);
        }
    }
}